// packageA/pages/service-detail/index.js
const WXAPI = require('../../api/service')
const baseUrl = require('../../../api/config').dev //这里用的是ES6的写法
import { handleResult } from "../../utils/tools";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeClick:false,
    currentPreviewImg: '',
    isCollect: false,
    showImage: false,
    serviceInfo: {},
    evaluateInfo: {},
    imgBaseUrl: `${baseUrl['imgBaseUrl']}`,
    currentId: '',
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 500
  },
  toOrderService() {
    let data = {
      iPrice: this.data.serviceInfo.iPrice,
      sName: this.data.serviceInfo.sName,
      sContent: this.data.serviceInfo.sContent,
      sFileUrl: this.data.imgBaseUrl + this.data.serviceInfo.sFileUrl,
      sProjectId: this.data.serviceInfo.sId
    }
    wx.navigateTo({
      url: `/packageA/pages/order-service/index?serviceObj=${JSON.stringify(data)}`,
    })
  },
  jumpAllEvaluate() {
    wx.navigateTo({
      url: '/packageA/pages/service-evaluate/index'
    })
  },
  onClose() {
    this.setData({
      showImage: !this.data.showImage
    })
  },
  async collect() {
    // 点击动画
    this.setData({
      activeClick:true
    })
    setTimeout(() => {
      this.setData({
        activeClick:false
      })
    }, 500);

    if (this.data.isCollect) {
      let res = await WXAPI.cancelCollect(this.data.currentId)
      if (res.data.code == 0) {
        this.setData({
          isCollect: false
        })
        wx.showToast({
          title: res.data.message,
        })
      }
    } else {
      let res = await WXAPI.sureCollect(this.data.currentId)
      if (res.data.code == 0) {
        this.setData({
          isCollect: true
        })
        wx.showToast({
          title: res.data.message,
        })
      }
    }
    
  },
  view(e) {
    this.setData({
      currentPreviewImg: e.currentTarget.dataset.url,
      showImage: true
    })
  },
  async getDetail() {
    let res = await WXAPI.getServiceDetail(this.data.currentId)
    if (res.data.code == 0) {
      res.data.data.sPublicityContent = unescape(res.data.data.sPublicityContent)
      this.setData({
        serviceInfo: handleResult(res.data.data),
        isCollect: res.data.data.iCollect == 0 ? false : true
      })
      console.log('res.data.data.iCollect',res.data.data.iCollect);
    }
  },
  async getEvaluate() {
    let params = {
      PageIndex: 1,
      PageSize: 1,
      iLoadImgUrl:1
    }
    let res = await WXAPI.getEvaluateList(this.data.currentId, params)
    if (res.data.code == 0) {
      this.setData({
        evaluateInfo: res.data.data.list ? res.data.data.list[0] : {}
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 存储服务项目id
    this.setData({
      currentId: options.id
    })
    wx.setNavigationBarTitle({
      title: options.title
    })
    // 服务详情
    this.getDetail()
    // 服务评价
    this.getEvaluate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})